package com.sanglh.tutorial.common.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

public class ComponentUtils {
	public static void enableComponent(Context context, Class<?> clazz,
			int flags) {
		if (context == null || clazz == null) {
			return;
		}

		PackageManager pm = context.getPackageManager();
		pm.setComponentEnabledSetting(new ComponentName(context, clazz),
				PackageManager.COMPONENT_ENABLED_STATE_ENABLED, flags);
	}

	public static void disableComponent(Context context, Class<?> clazz,
			int flags) {
		if (context == null || clazz == null) {
			return;
		}
		PackageManager pm = context.getPackageManager();
		pm.setComponentEnabledSetting(new ComponentName(context, clazz),
				PackageManager.COMPONENT_ENABLED_STATE_DISABLED, flags);
	}

	public static void enableComponent(Context context, Class<?> clazz) {
		enableComponent(context, clazz, PackageManager.DONT_KILL_APP);
	}

	public static void disableComponent(Context context, Class<?> clazz) {
		disableComponent(context, clazz, PackageManager.DONT_KILL_APP);
	}

	public static boolean isComponentEnabled(Context context, Class<?> clazz) {
		if (context == null || clazz == null) {
			return false;
		}

		PackageManager pm = context.getPackageManager();
		int state = pm.getComponentEnabledSetting(new ComponentName(context,
				clazz));

		return state != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
	}

	public static void setComponentEnabled(Context context, Class<?> clazz,
			boolean enable) {
		if (enable) {
			enableComponent(context, clazz);
		} else {
			disableComponent(context, clazz);
		}
	}

	public static Bitmap getComponentIcon(Context context, String pkg,
			String clazz) {
		try {
			Drawable icon;

			if (TextUtils.isEmpty(clazz)) {
				icon = context.getPackageManager().getApplicationIcon(pkg);
			} else {
				ComponentName component = new ComponentName(pkg, clazz);
				icon = context.getPackageManager().getActivityIcon(component);
			}

			if (icon != null && icon instanceof BitmapDrawable) {
				return ((BitmapDrawable) icon).getBitmap();
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}

		return null;
	}

	public static Intent getLaunchIntent(Context context, String pkg,
			String clazz) {
		Intent intent;

		if (TextUtils.isEmpty(clazz)) {
			intent = context.getPackageManager().getLaunchIntentForPackage(pkg);
		} else {
			intent = new Intent().setComponent(new ComponentName(pkg, clazz));
		}

		if (intent != null) {
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		}

		return intent;
	}

	public static String getName(Context context, String pkg, String clazz) {
		try {
			PackageManager pm = context.getPackageManager();

			if (TextUtils.isEmpty(clazz)) {
				return pm.getApplicationInfo(pkg, 0).loadLabel(pm).toString();
			} else {
				ActivityInfo info = pm.getActivityInfo(new ComponentName(pkg,
						clazz), 0);
				return info.loadLabel(pm).toString();
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}

		return null;
	}

	public static String getAppVersionName(String pkg, Context context) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			return info.versionName;
		} catch (Exception e) {
			return "";
		}
	}
}
